package com.treasure.infrastructure.domain.llm.qianwen;

import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.treasure.domain.domain.LargeLanguageModelDomainService;
import com.treasure.common.exception.ServiceException;
import com.treasure.common.types.Prompt;
import com.treasure.common.model.command.ConversationRequest;
import com.treasure.common.model.ChatHistoryEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.treasure.infrastructure.domain.llm.qianwen.client.QwenGenerationClient;

import java.util.List;

/**
 * @author sunhongmin
 * @date 2023/8/27 09:50
 * @description
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LargeLanguageModelDomainServiceImpl implements LargeLanguageModelDomainService {
    private final QwenGenerationClient qwenGenerationClient;

    @Override
    public String callGeneration(Prompt prompt) {
        GenerationResult generationResult;
        try {
            generationResult = qwenGenerationClient.qwenGeneration(prompt.getValue());
        } catch (NoApiKeyException e) {
            log.error("callGeneration NoApiKeyException e: ", e);
            throw new ServiceException(e.getMessage());
        } catch (InputRequiredException e) {
            log.error("callGeneration InputRequiredException e: ", e);
            throw new ServiceException(e.getMessage());
        }

        return generationResult.getOutput().getText();
    }


    @Override
    public String callGeneration(ConversationRequest conversationRequest) throws NoApiKeyException, InputRequiredException {
        return qwenGenerationClient.doGeneration(conversationRequest);
    }


}
